﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using diploma.core.data;

namespace diploma.core.data
{
    public enum DataActions : int
    {
        get = 0,
        getAll = 1,
        insert = 2,
        delete = 3,
        update = 4
    }

    public class DataRouter
    {
        private DataRouter() { }

        private DataSource dataSource = null;

        private static DataRouter instance = null;
        public static DataRouter Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DataRouter();
                }
                return instance;
            }
        }

        public void AttachDataSource(DataSource _dataSource)
        {
            dataSource = _dataSource;
        }

        public bool hasDataSource
        {
            get
            {
                return dataSource != null;
            }
        }

        public void Act(DataActions _action, DataObjectTypes _dataObjectType, IDataObject dataObject)
        {
            switch (_action)
            {
                case DataActions.getAll:
                    dataSource.MakeAction(_dataObjectType, _action);
                    break;
                case DataActions.get:
                case DataActions.delete:
                    dataSource.MakeAction(_dataObjectType, _action, dataObject.Id);
                    break;
                case DataActions.insert:
                case DataActions.update:
                    dataSource.MakeAction(_dataObjectType, _action, dataObject);
                    break;
            }
        }

        private static object lastReturnObject = null;
        public static object LastReturn
        {
            get
            {
                return lastReturnObject;
            }
            set
            {
                lastReturnObject = value;
            }
        }

    }
}
